/*
 *  tropeTypes.cpp
 *  tropeProject
 *
 *  Created by Jared Schiffman on 12/5/10.
 *  Copyright 2010 Potion. All rights reserved.
 *
 */

#include "tropeTypes.h"

std::vector<tropeType*>				tropeType::masterTypeList;

tropeType::tropeType( std::string _name, tropeMetaType	_metaType, tropeTypeID _parentTypeID )
{
	name = _name;
	metaType = _metaType;
	parentTypeID = _parentTypeID;
	
	registerType(this);
}


#pragma mark STATIC TYPE LOOKUP FUNCTIONS

tropeType*	tropeType::getType( std::string typeName )
{
	for( int i=0; i<masterTypeList.size(); i++ )
		if ( masterTypeList[i]->name == typeName )
			return masterTypeList[i];
	return NULL;
}


tropeType*		tropeType::getType( tropeTypeID ID )
{
	if ( ID < 0 || ID > masterTypeList.size() )
		return masterTypeList[ID];
	return NULL;
}


tropeTypeID		tropeType::getTypeID( std::string typeName )
{
	for( int i=0; i<masterTypeList.size(); i++ )
		if ( masterTypeList[i]->name == typeName )
			return (tropeTypeID)i;
	return TROPE_NULL_TYPE;
}


int		tropeType::registerType( tropeType* T )
{
#pragma todo make sure types are only registered once
	masterTypeList.push_back( T );
	T->typeID = masterTypeList.size()-1 ;
	return T->typeID;
}


void	tropeType::makeBaseTypes()
{
	new tropeType( "null_type", TROPE_BUILT_IN_TYPE, 0 );
	new tropeType( "int", TROPE_BUILT_IN_TYPE, 0 );
	new tropeType( "bool", TROPE_BUILT_IN_TYPE, 0 );
	new tropeType( "float", TROPE_BUILT_IN_TYPE, 0 );
	new tropeType( "string", TROPE_BUILT_IN_TYPE, 0 );
}


